<?php
/**
 * 提货产品申请
 * (model)
 */
namespace App\Models\Shipping;

use Illuminate\Database\Eloquent\Model;
class SSkuApply extends Model
{
    protected $table = 's_shipping_sku_apply';

    protected $guarded = [];

    public $timestamps = false;

    const SHIPPING_NUM = 22;//周期数

    /**
     * 发货日期（获取当前周期，往后推N个周期）
     * @param $day
     * @return array
     */
    public static function initShippingDate($day = null){
        $day = $day ?: date('j');
        $date = [];
        $j = self::SHIPPING_NUM / 2;
        if($day <= 15){
            $date[date('Y-m-01')] = date('y/m').'上';
            $date[date('Y-m-16')] = date('y/m').'下';
        }else{
            $time = strtotime('midnight first day of +'.$j.' month');
            $date[date('Y-m-16')] = date('y/m').'下';
            $date[date('Y-m-01',$time)] = date('y/m',$time).'上';
        }
        for ($i = 1; $i <= $j-1; $i++){
            $_time = strtotime('midnight first day of +'.$i.' month');
            $date[date('Y-m-01',$_time)] = date('y/m',$_time).'上';
            $date[date('Y-m-16',$_time)] = date('y/m',$_time).'下';
        }
        ksort($date);
        return $date;
    }

    /**
     * 提货/申请数量
     * @param $areaId
     * @param $pSkuId
     * @param $sDate
     * @param $eDate
     * @return array
     */
    public static function getApplyNum($areaId,$pSkuId,$sDate,$eDate){
        return self::query()
            ->where('area_id',$areaId)
            ->where('psku_id',$pSkuId)
            ->where('shipping_date','>=',$sDate)
            ->where('shipping_date','<=',$eDate)
            ->pluck('apply_num','shipping_date')
            ->toArray();
    }
}
